package com.sakura.mall.coupon.couponHistory.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.coupon.couponHistory.domain.entity.CouponHistoryEntity;
import com.sakura.mall.coupon.couponHistory.domain.request.AddCouponHistoryRequest;
import com.sakura.mall.coupon.couponHistory.domain.request.UpdateCouponHistoryRequest;
import com.sakura.mall.coupon.couponHistory.domain.request.CouponHistoryPageRequest;
import com.sakura.mall.coupon.couponHistory.domain.vo.CouponHistoryVo;
import com.sakura.mall.coupon.couponHistory.mapper.CouponHistoryMapper;
import com.sakura.mall.coupon.couponHistory.service.CouponHistoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 优惠券领取历史记录 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class CouponHistoryServiceImpl extends ServiceImpl<CouponHistoryMapper, CouponHistoryEntity> implements CouponHistoryService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddCouponHistoryRequest request) {
        // todo 其他校验逻辑
        CouponHistoryEntity couponHistory = request.convert(CouponHistoryEntity.class);
        this.save(couponHistory);
    }

    @Override
    public Page<CouponHistoryVo> queryPage(CouponHistoryPageRequest request) {
        IPage<CouponHistoryEntity> qp = request.toPage();
        LambdaQueryWrapper<CouponHistoryEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<CouponHistoryVo> ret = qp.convert(entity -> entity.convert(CouponHistoryVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateCouponHistoryRequest request) {
        CouponHistoryEntity entity = request.convert(CouponHistoryEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

